﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.RouteSetting
{
    public partial class FrmRoute : Form
    {
        public dl_route Route { get; set; }
        private List<rm_room_info> roomList = new List<rm_room_info>();
        private List<relieve_area_info> relieveList = new List<relieve_area_info>();
        private List<building_location> locationList = new List<building_location>();

        public FrmRoute()
        {
            InitializeComponent();

            InitData();

            this.cmbStartType.SelectedIndex = 0;
            this.cmbEndType.SelectedIndex = 0;
        }

        private void InitData()
        {
            cmbRouteType.Items.Clear();
            string[] routeType = Enum.GetNames(typeof(DownloadTaskRouteType));
            foreach (string t in routeType)
            {
                cmbRouteType.Items.Add(t);
            }

            var ret_room = HttpHelper.Get<HResult<List<rm_room_info>>>(AppSettings.Setting.API.GetFreeRoom);

            if (ret_room.success)
            {
                roomList = ret_room.data;
            }

            var ret_relieve = HttpHelper.Get<HResult<List<relieve_area_info>>>(AppSettings.Setting.API.GetRelieveAreaList);

            if (ret_relieve.success)
            {
                relieveList = ret_relieve.data;
            }

            var ret_location = HttpHelper.Get<HResult<List<building_location>>>(AppSettings.Setting.API.GetBuildingLocationList);

            if (ret_location.success)
            {
                locationList = ret_location.data;
            }
        }

        public void SetRoute(dl_route route)
        {
            if (route != null)
            {
                Route = route;

                txtName.Text = route.name;
                cmbRouteType.SelectedIndex = route.route_type;
                cmbStartType.SelectedIndex = route.start_location_type;
                cmbStartName.SelectedItem = route.start_location_name;
                cmbEndType.SelectedIndex = route.end_location_type;
                cmbEndName.SelectedItem = route.end_location_name;
            }
        }

        private void cmbStartType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbStartName.Items.Clear();

            if (cmbStartType.SelectedIndex == 0)
            {
                foreach (var location in locationList)
                {
                    cmbStartName.Items.Add(location.location_name);
                }
            }
            else if (cmbStartType.SelectedIndex == 1)
            {
                foreach (var area in relieveList)
                {
                    cmbStartName.Items.Add(area.area_name);
                }
            }
            else if (cmbStartType.SelectedIndex == 2)
            {
                foreach (var room in roomList.Where(t => t.is_normal.Value == 2).ToList())
                {
                    //谈话间
                    cmbStartName.Items.Add(room.room_number);
                }
            }
           
        }

        private void cmbEndType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbEndName.Items.Clear();

            if (cmbEndType.SelectedIndex == 0)
            {
                foreach (var location in locationList)
                {
                    cmbEndName.Items.Add(location.location_name);
                }
            }
            else if (cmbEndType.SelectedIndex == 1)
            {
                foreach (var area in relieveList)
                {
                    cmbEndName.Items.Add(area.area_name);
                }
            }
            else if (cmbEndType.SelectedIndex == 2)
            {
                foreach (var room in roomList.Where(t => t.is_normal.Value == 2).ToList())
                {
                    cmbEndName.Items.Add(room.room_number);
                }
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (Route == null)
            {
                Route = new dl_route();
                Route.id = Guid.NewGuid().ToString();
            }

            string startId = "";
            string endId = "";

            if (cmbStartType.SelectedIndex == 0)
            {
                startId = locationList[cmbStartName.SelectedIndex].id;
            }
            else if (cmbStartType.SelectedIndex == 1)
            {
                startId = relieveList[cmbStartName.SelectedIndex].id;
            }
            else
            {
                startId = roomList.Where(t => t.room_number == cmbStartName.SelectedItem.ToString()).FirstOrDefault().id;
            }

            if (cmbEndType.SelectedIndex == 0)
            {
                endId = locationList[cmbEndName.SelectedIndex].id; 
            }
            else if (cmbEndType.SelectedIndex == 1)
            {
                endId = relieveList[cmbEndName.SelectedIndex].id;
            }
            else
            {
                endId = roomList.Where(t => t.room_number == cmbEndName.SelectedItem.ToString()).FirstOrDefault().id;
            }

            Route.name = txtName.Text.Trim();
            Route.route_type = cmbRouteType.SelectedIndex;
            Route.start_location_id = startId;
            Route.start_location_type = cmbStartType.SelectedIndex;
            Route.start_location_name = cmbStartName.SelectedItem.ToString();
            Route.end_location_id = endId;
            Route.end_location_type = cmbEndType.SelectedIndex;
            Route.end_location_name = cmbEndName.SelectedItem.ToString();

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Route = null;
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
